<?php

namespace app\shop\controller;

class Mall extends Base
{

    // 商品列表
    public function goods()
    {
        if (is_post()) {
			$post = input('post.');
			if (input('get.act') == 'opt') {
				return model('Goods')->modifyStatus($post['id'], $post);
			}
			// 设置热销
			if (input('get.act') == 'hot') {
				if (db('goods')->where('id', $post['id'])->setField('is_hot', $post['status']))
					$this->success('操作完成');
				$this->error('稍后重试');
			}
		}

		$map = [];
		// $map['status'] = 1;
		$map['shop_id'] = session('shopid');
		if(is_get()){
			if(is_numeric(input('get.status'))){
				$map['status'] = input('get.status');
			}
			if(input('get.cate_id')){
				$map['cate_id'] = input('get.cate_id');
			}
			if(input('get.keywords')){
				$map['goods_name'] = ['like','%'.input('get.keywords').'%'];
			}
		}

		$cate = db('goods_cate')->select();
		// foreach ($cate as $k => $v) {
		// 	$cate[$k]['son'] = db('goods_cate')->where('pid',$v['id'])->select();
		// }
		$this->assign('cate',$cate);
		$this->assign('list',model('Goods')->getList($map));
		return $this->fetch();
    }
    
	// 添加修改商品信息
	public function addGoods()
	{
		if(is_post()){
			$post = input('post.');
			$post['status'] = 0;
			$post['shop_id'] = session('shopid');

			$validate = validate('Vgoods');
			$rst = $validate->check($post);
			if (! $rst) {
	            $this->error($validate->getError());
	        } else {
	        	if (input('post.id')) {
					$rst = model('Goods')->editInfo($post);
				} else {
					$rst = model('Goods')->addInfo($post);
				}
	        }
	        return $rst;
		}

		$goodsInfo['goods_sku'] = [];
		if(input('get.id')){
			$goodsInfo = model('Goods')->getInfo(input('get.id'));
		}
		$this->assign('info', $goodsInfo);

		$cate = db('goods_cate')->where('pid',0)->select();

		// foreach ($cate as $k => $v) {
		// 	$cate[$k]['son'] = db('goods_cate')->where('pid',$v['id'])->select();
		// }
		$this->assign('cate',$cate);
		return $this->fetch();
    }

     // 移除sku
    public function rmSku()
    {
    	$skuid = input('get.skuid');

    	if (db('goods_sku')->where('sku_id', $skuid)->delete()) {
    		$this->success('已移除');
    	}

    	$this->error('稍后重试');
    }
    
	// 商城订单
	public function orderList()
	{
		$map = [
			'shop_id' => session('shopid'),
			'is_pay' => 1
		];
		$where = [];
		if (is_get()) {
			if (is_numeric(input('get.status'))) {
				$map['status'] = input('get.status');
			}
			if (is_numeric(input('get.is_pay'))) {
				$map['is_pay'] = input('get.is_pay');
			}
			if (input('get.keywords')) {
				$where['name'] = ['like', '%'.input('get.keywords').'%'];
			} 
		}

		$list = model('Order')->getList($map);
		$this->assign('list', $list);

		return $this->fetch();
	}

	// 订单详情
	public function orderDetail()
	{
		if (input('get.id')) {

			$orderId = input('get.id');

			$info = model('Order')->getInfo($orderId);
			$this->assign('info',$info);
		}
		return $this->fetch();
	}

	// 确认发货
	public function fahuo()
	{
		if (input('post.id')) {
			$rst = db('order')->where('id', input('post.id'))->update([
				'status' => 1,
				'shipping_no' => input('post.no')
			]);	
			if ($rst) {
				$this->success('发货成功！');
			}
			$this->error('操作失败');
		}
		$this->error('订单不存在！');
	}

	// 店家驳回订单
	public function reject()
	{
		if (input('post.id')) {
			$rst = model('Order')->reject(input('post.id'),'商家'.session('shop_name').'订单驳回');

			$this->success($rst);
		}
		$this->error('订单不存在！');
	}



}
